package com.klhappy.houyuan.utils;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import android.text.TextUtils;

import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Request.Builder;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

public class HttpTool {
	
	private static  OkHttpClient client = new OkHttpClient();

	public static Response post(String url) throws IOException {
		return post(url, null);
	}
	
	public static  Response post(String url, Map<String, String> postMap) throws IOException {
	    return post(url, null, postMap);
	}
	
	public static Response post(String url, String cookie, Map<String, String> postMap) throws IOException {
		
		Builder  requestBulider = new Request.Builder().url(url);
		
		if(!TextUtils.isEmpty(cookie)) {
			requestBulider = requestBulider.header("Cookie", cookie);
		}
		
		if(postMap != null) {
			Set<String> keySet = postMap.keySet();
			Iterator<String> iterator = keySet.iterator();
			FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();
			while(iterator.hasNext()) {
				String key = iterator.next();
				String value = postMap.get(key);
				formEncodingBuilder.add(key, value);
			}
			RequestBody formBody = formEncodingBuilder.build();
			requestBulider = requestBulider.post(formBody);
		}
		
		return client.newCall(requestBulider.build()).execute();
	}
}
